#include "socket.h"
#include "error.h"

#include <sys/socket.h>
#include <type_traits>

namespace _ {

static_assert(std::is_same_v<Wrap::socklen_t_, socklen_t>, "socketlen_t");

}

int Wrap::accept_(int fd, struct sockaddr *addr, socklen_t_ *addrLen) {
    return PERROR_NEG(::accept(fd, addr, addrLen));
}

int Wrap::accept4_(int fd, struct sockaddr *addr, socklen_t_ *addrLen, int flags) {
    return PERROR_NEG(::accept4(fd, addr, addrLen, flags));
}

int Wrap::bind_(int fd, const struct sockaddr *addr, socklen_t_ len) {
    return PERROR_NEQ0(::bind(fd, addr, len));
}

int Wrap::connect_(int fd, const struct sockaddr *addr, socklen_t_ len) {
    return PERROR_NEQ0(::connect(fd, addr, len));
}

int Wrap::getpeername_(int fd, struct sockaddr *addr, socklen_t_ *len) {
    return PERROR_NEQ0(::getpeername(fd, addr, len));
}

int Wrap::getsockname_(int fd, struct sockaddr *addr, socklen_t_ *len) {
    return PERROR_NEQ0(::getsockname(fd, addr, len));
}

int Wrap::getsockopt_(int fd, int level, int optname, void *optval, socklen_t_ *optlen) {
    return PERROR_NEQ0(::getsockopt(fd, level, optname, optval, optlen));
}

int Wrap::listen_(int fd, int n) {
    return PERROR_NEQ0(::listen(fd, n));
}

ssize_t Wrap::recv_(int fd, void *buf, size_t n, int flags) {
    return PERROR_NEG(::recv(fd, buf, n, flags));
}

ssize_t Wrap::send_(int fd, const void *buf, size_t n, int flags) {
    return PERROR_NEG(::send(fd, buf, n, flags));
}

int Wrap::setsockopt_(int fd, int level, int optname, const void *optval, socklen_t_ optlen) {
    return PERROR_NEQ0(::setsockopt(fd, level, optname, optval, optlen));
}

int Wrap::shutdown_(int fd, int how) {
    return PERROR_NEQ0(::shutdown(fd, how));
}

int Wrap::socket_(int domain, int type, int protocol) {
    return PERROR_NEG(::socket(domain, type, protocol));
}


